package com.lson.MQ;

import com.lson.TestApplication;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;
//import org.springframework.amqp.core.Message;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;

//@Component
public class RabbitMqService {
    @Autowired
    RabbitTemplate rabbitTemplate;
    // 队列名称
    public final static String SPRING_BOOT_QUEUE = "spring-boot-queue-1";
    // 交换机名称
    public final static String SPRING_BOOT_EXCHANGE = "spring-boot-exchange-1";
    // 绑定的值
    public static final String SPRING_BOOT_BIND_KEY = "spring-boot-bind-key-1";
    @RabbitHandler
    @RabbitListener(queues = RabbitMqService.SPRING_BOOT_QUEUE)
    public void receive1(String message,Channel channel,@Header(AmqpHeaders.DELIVERY_TAG) long tag){
        System.out.println("HelloReceiver收到  : " + message +"收到时间"+new Date());
        try {
            //告诉服务器收到这条消息 已经被我消费了 可以在队列删掉 这样以后就不会再发了 否则消息服务器以为这条消息没处理掉 后续还会在发
            Thread.sleep(1000*5);
            channel.basicAck(tag,false);
            System.out.println("receiver success");
        } catch (Exception e) {
            e.printStackTrace();
            //丢弃这条消息
            //channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,false);
            System.out.println("receiver fail");
        }



    }


    public void send(String msg){
//        rabbitTemplate.
//        rabbitTemplate.convertAndSend(SPRING_BOOT_EXCHANGE,SPRING_BOOT_BIND_KEY,msg);
    }



}
